package example.json.msg.impl;

import com.dahantc.api.commons.EncryptUtil;
import com.dahantc.api.voice.json.HttpJSONClient;
import com.dahantc.api.voice.json.VoiceReqData;
import example.json.msg.MsgBaseService;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import net.sf.json.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

import static example.json.msg.util.PhoneNumberUtil.*;

@SuppressWarnings("all")
@Repository
@Data
@Setter
@Getter
public class DahanExample extends BaseHelper implements MsgBaseService {

    @Override
    public String batchSend(String accType, String phones, String content) {
        JSONObject ddata = new JSONObject();
        if (StringUtils.equalsIgnoreCase(accType, "yx")) {
            ddata.put("account", dahan_yx_acc);
            ddata.put("password", EncryptUtil.MD5Encode(dahan_yx_pwd));
        } else {
            ddata.put("account", dahan_hy_acc);
            ddata.put("password", EncryptUtil.MD5Encode(dahan_hy_pwd));
        }
        ddata.put("msgid", uuid());
        if (StringUtils.isNotEmpty(phones)) {
            ddata.put("phones", phones);
        } else {
            ddata.put("phones", phone(1));
        }
        if (StringUtils.isNotEmpty(content)) {
            ddata.put("content", content);
        } else {
            ddata.put("content", context());
        }
        ddata.put("sign", sign(content));
        ddata.put("subcode", subcode(accType, content));
        ddata.put("sendtime", sendtime());

        return httpClient(dahan_master_url + dahan_send_url)
                .doPostJson(ddata);
    }

    @Override
    public String mutipleSend(String accType, String content) {
        JSONObject ddata = new JSONObject();
        if (StringUtils.equalsIgnoreCase(accType, "yx")) {
            ddata.put("account", dahan_yx_acc);
            ddata.put("password", EncryptUtil.MD5Encode(dahan_yx_pwd));
        } else {
            ddata.put("account", dahan_hy_acc);
            ddata.put("password", EncryptUtil.MD5Encode(dahan_hy_pwd));
        }
        if (StringUtils.isNotEmpty(content)) {
            ddata.put("contentArr", dahanRequestBody(accType, content));
        } else {
            ddata.put("contentArr", dahanRequestBody(accType));
        }

        return httpClient(dahan_master_url + dahan_batch_send_url)
                .doPostJson(ddata);
    }

    @Override
    public String voice(String accType, String phones, String content) {
        String result = "ok";
        try {
            HttpJSONClient client = new HttpJSONClient(dahan_voice_master_url);

            List<VoiceReqData> list = new ArrayList<VoiceReqData>();

            for (String phone : phones.split(",")) {
                list.add(new VoiceReqData(phone, content, "", uuid(), 1, 0));
            }
            //list.add(new VoiceReqData("13812345678", "211457", "dh9876tishiyin", "", 1, 0));
            result = client.sendAuthCodeVoiceSms(dahan_voice_acc, dahan_voice_pwd, list);
            System.out.println(result);

            String reportResp = client.getVoiceReport(dahan_voice_acc, dahan_voice_pwd);

            System.out.println(reportResp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public String report(String accType){
        JSONObject ddata = new JSONObject();
        if (StringUtils.equalsIgnoreCase(accType, "yx")) {
            ddata.put("account", dahan_yx_acc);
            ddata.put("password", EncryptUtil.MD5Encode(dahan_yx_pwd));
        } else {
            ddata.put("account", dahan_hy_acc);
            ddata.put("password", EncryptUtil.MD5Encode(dahan_hy_pwd));
        }

        return httpClient(dahan_master_url + dahan_get_report_url)
                .doPostJson(ddata);
    }

    @Override
    public String sms(String accType){
        JSONObject ddata = new JSONObject();
        if (StringUtils.equalsIgnoreCase(accType, "yx")) {
            ddata.put("account", dahan_yx_acc);
            ddata.put("password", EncryptUtil.MD5Encode(dahan_yx_pwd));
        } else {
            ddata.put("account", dahan_hy_acc);
            ddata.put("password", EncryptUtil.MD5Encode(dahan_hy_pwd));
        }

        return httpClient(dahan_master_url + dahan_deliver_url)
                .doPostJson(ddata);
    }

    @Override
    public String balance(String accType){
        JSONObject ddata = new JSONObject();
        if (StringUtils.equalsIgnoreCase(accType, "yx")) {
            ddata.put("account", dahan_yx_acc);
            ddata.put("password", EncryptUtil.MD5Encode(dahan_yx_pwd));
        } else {
            ddata.put("account", dahan_hy_acc);
            ddata.put("password", EncryptUtil.MD5Encode(dahan_hy_pwd));
        }

        return httpClient(dahan_master_url + dahan_balance_url)
                .doPostJson(ddata);
    }

    /** 1.2 大汉 N-N 生成短信内容 */
    public JSONArray dahanRequestBody(String accType) {
        return dahanRequestBody(accType, null);
    }

    public JSONArray dahanRequestBody(String accType, String str){
        JSONArray array = new JSONArray();
        if (StringUtils.isNotEmpty(str)) {
            //内容格式：15010254294,内容1|18911551425,内容2
            for (String msg : str.split("|")) {
                String content = msg.split(",")[1];
                //1.2.1 第1类短信
                JSONObject obj1 = new JSONObject();
                obj1.put("msgid", uuid());
                obj1.put("phones", msg.split(",")[0]);
                obj1.put("content", content);
                obj1.put("sign", sign(content));
                obj1.put("subcode", subcode(accType, content));
                obj1.put("sendtime", sendtime());
                array.add(obj1);
            }
        } else {
            String content = "【钱站】尊敬的用户，您优质的信用为您匹配，有车就能申请借款的产品。";
            //1.2.1 第1类短信
            JSONObject obj1 = new JSONObject();
            obj1.put("msgid", uuid());
            obj1.put("phones", "15010254294");
            obj1.put("content", content);
            obj1.put("sign", sign(content));
            obj1.put("subcode", subcode(accType, content));
            obj1.put("sendtime", sendtime());
            array.add(obj1);

            JSONObject obj2 = new JSONObject();
            obj2.put("msgid", uuid());
            obj2.put("phones", "15010254294");
            obj2.put("content", content);
            obj2.put("sign", sign(content));
            obj2.put("subcode", subcode(accType, content));
            obj2.put("sendtime", sendtime());
            array.add(obj2);
        }

        return array;
    }

    private String sign(String content){
        try {
            if (StringUtils.contains(content, "【爱钱进】")) {
                return new String(dahan_aqj_sign.getBytes("ISO-8859-1"), "UTF-8");
            } else if (StringUtils.contains(content, "【钱站】")) {
                return new String(dahan_qz_sign.getBytes("ISO-8859-1"), "UTF-8");
            } else if (StringUtils.contains(content, "【凡普信】")) {
                return new String(dahan_fpx_sign.getBytes("ISO-8859-1"), "UTF-8");
            } else if (StringUtils.contains(content, "【任买】")) {
                return new String(dahan_rm_sign.getBytes("ISO-8859-1"), "UTF-8");
            } else {
                return new String("aiqianjin");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return "aiqian";
        }
    }


    private String subcode(String accType, String content){
        String subcode = dahan_aqj_hy_subcode;
        if (StringUtils.contains(accType, "hy") && StringUtils.contains(content, "【爱钱进】")) {
            subcode = dahan_aqj_hy_subcode;
        } else if (StringUtils.contains(accType, "yx") && StringUtils.contains(content, "【爱钱进】")){
            subcode = dahan_aqj_yx_subcode;
        } else if (StringUtils.contains(accType, "hy") && StringUtils.contains(content, "【钱站】")){
            subcode = dahan_qz_hy_subcode;
        } else if (StringUtils.contains(accType, "yx") && StringUtils.contains(content, "【钱站】")){
            subcode = dahan_qz_yx_subcode;
        } else if (StringUtils.contains(accType, "hy") && StringUtils.contains(content, "【凡普信】")){
            subcode = dahan_fpx_hy_subcode;
        } else if (StringUtils.contains(accType, "hy") && StringUtils.contains(content, "【任买】")){
            subcode = dahan_rm_hy_subcode;
        }
        return subcode;
    }
}
